package com.xjrsoft.core.tool.utils;

import com.aliyun.alimt20181012.Client;
import com.aliyun.alimt20181012.models.TranslateGeneralRequest;
import com.aliyun.alimt20181012.models.TranslateGeneralResponse;
import com.aliyun.alimt20181012.models.TranslateRequest;
import com.aliyun.alimt20181012.models.TranslateResponse;
import com.aliyun.tea.TeaException;
import com.xjrsoft.common.config.AiplmConfig;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TranslateUtil {

    public static Client createClient() throws Exception {
        AiplmConfig aiplmConfig = SpringUtil.getBean(AiplmConfig.class);
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(aiplmConfig.getAliAccess())
                .setAccessKeySecret(aiplmConfig.getAliSecret());
        config.endpoint = "mt.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }
    /**
     * 阿里云通用版中译英
     * @param text
     * @return
     */
    public static String aliGeneralTranslate(String text){
        try {
            Client client = createClient();
            TranslateGeneralRequest translateGeneralRequest = new TranslateGeneralRequest()
                    .setSourceLanguage("zh")
                    .setFormatType("text")
                    .setTargetLanguage("en")
                    .setSourceText(text)
                    .setScene("general");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            TranslateGeneralResponse response = client.translateGeneralWithOptions(translateGeneralRequest, runtime);
            return response.body.data.translated;
        } catch (TeaException error) {
        } catch (Exception _error) {
        }
        return null;
    }

    /**
     * 阿里云专业版中译英
     * @param text
     * @return
     */
    public static String aliEcommerceTranslate(String text){
        try {
            Client client = createClient();
            TranslateRequest translateRequest = new TranslateRequest()
                    .setFormatType("text")
                    .setTargetLanguage("en")
                    .setSourceLanguage("zh")
                    .setSourceText(text)
                    .setScene("description");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            TranslateResponse response = client.translateWithOptions(translateRequest, runtime);
            return response.body.data.translated;
        } catch (TeaException error) {
        } catch (Exception _error) {
        }
        return null;
    }
}
